﻿<%@ WebHandler Language="C#" Class="Site" %>
using System;
using System.Web;
using Company.StudyClub.BLL.Encrypt;
using Company.StudyClub.BLL.Common;
using Company.StudyClub.BLL.User;
using Company.StudyClub.BLL.Site;
using System.Collections.Generic;
using System.Text;
using Company.StudyClub.Model;

public class Site : IHttpHandler {
    private HttpHelper helper = new HttpHelper();
    public void ProcessRequest(HttpContext context)
    {
        HttpResponse response = context.Response;
        switch (helper.GetString("type").ToLower())
        {
            case "getcity":
                getRegionInfo(response);
                break;
            case "add":
                AddPro(response);
                break;
            case "del":
                DelRegion(response);
                break;
            case "update":
                UpdateRegion(response);
                break;
            case "sorting":
                DoSorting(response);
                break;
            default: break;
        }
    }

    /// <summary>
    /// 获得地区的子地区
    /// </summary>
    /// <param name="response"></param>
    private int GetChildNum(int parentId)
    {
        RegionBLL objBLL = new RegionBLL();
        List<Site_Region> objList = new List<Site_Region>();
        StringBuilder objSB = new StringBuilder();
        objList = objBLL.getRegionList(parentId);
        if (objList.Count > 0)
        {
            return objList.Count;
        }
        else
        {
            return 0;
        }
    }

    /// <summary>
    /// 删除地区
    /// </summary>
    /// <param name="response"></param>
    private void DelRegion(HttpResponse response)
    {
        try
        {
            string[] sids = helper.GetString("sids").Split(',');
            for (int i = 0; i < sids.Length; i++)
            {
                if (sids[i] == string.Empty)
                    continue;

                RegionBLL objBLL = new RegionBLL();
                objBLL.Delete(int.Parse(sids[i]));
            }
            response.Write("1");
        }
        catch
        {
            response.Write("0");
        }
        
    }

    /// <summary>
    /// 更新地区信息
    /// </summary>
    /// <param name="response"></param>
    private void UpdateRegion(HttpResponse response)
    {
        int opid = helper.GetInt("opid");
        int pid = helper.GetInt("upid");
        int rid = helper.GetInt("urid");

        RegionBLL objBLL = new RegionBLL();
        string newkeywords = string.Empty;

        if (pid > 0)
        {
            Site_Region rmodel = objBLL.GetModel(pid);

            if (rmodel.Keywords.IndexOf("/" + rid + "/") >= 0)
            {
                response.Write("-1");
                return;
            }
            newkeywords = rmodel.Keywords;
        }

        Site_Region model = objBLL.GetModel(rid);
        if (model == null)
        {
            response.Write("0");
            return;
        }
        model.RegionName = helper.GetString("uname");
        
        if (pid != model.ParentID)
        {
            model.ParentID = pid;
        }
        objBLL.Update(model);

        if (pid != opid)
        {

            //update keyswords
            if (newkeywords.Length == 0)
            {
                newkeywords = "/" + model.RegionID + "/";
            }
            else
            {
                newkeywords += model.RegionID + "/";
            }

            bool result = objBLL.UpdateKeyWords(model.Keywords, newkeywords);

            if (!result)
            {
                response.Write("2");
                return;
            }
        }
        response.Write("1");
    }
    
    /// <summary>
    /// 添加地区
    /// </summary>
    /// <param name="response"></param>
    private void AddPro(HttpResponse response)
    {
        try
        {
            Site_Region model = new Site_Region();
            int pid = helper.GetInt("apid");
            string rname = helper.GetString("aname");
            model.ParentID = pid;
            model.RegionName = rname;
            model.Sequence = -1;
            model.AreType = -1;
            RegionBLL objBLL = new RegionBLL();
            model.RegionID=objBLL.Add(model);
            if (model.ParentID > 0)
            {
                Site_Region rmodel = objBLL.GetModel(model.ParentID);
                model.Keywords = rmodel.Keywords + model.RegionID + "/";
            }
            else
            {
                model.Keywords = "/" + model.RegionID + "/";
            }
            objBLL.Update(model);
            response.Write(model.RegionID.ToString());
        }
        catch
        {
            response.Write("-1");
        }

    }

    
    /// <summary>
    /// 获得地区信息
    /// </summary>
    /// <param name="response"></param>
    private void getRegionInfo(HttpResponse response)
    {
        int pid = helper.GetInt("pid");
        RegionBLL objBLL = new RegionBLL();
        List<Site_Region> objList = new List<Site_Region>();
        StringBuilder objSB = new StringBuilder();
        objList = objBLL.getRegionList(pid);
         objSB.Append("[");
        for (int i = 0; i < objList.Count; i++)
        {
            Site_Region model = (Site_Region)objList[i];
            objSB.Append("{\"rid\":\"");
            objSB.Append(model.RegionID);
            objSB.Append("\",\"rname\":\"");
            objSB.Append(model.RegionName);
            objSB.Append("\",\"keywords\":\"");
            objSB.Append(model.Keywords);
            objSB.Append("\",\"seq\":\"");
            objSB.Append(model.Sequence);
            objSB.Append("\",\"childnum\":\"");
            objSB.Append(GetChildNum(model.RegionID));
            objSB.Append("\"}");
            if (i!= objList.Count -1)              
                objSB.Append(",");
        }
        objSB.Append("]");
        response.Write(objSB.ToString());
    }


    private void DoSorting(HttpResponse response)
    {
        RegionBLL objBLL = new  RegionBLL();

        try
        {
            string[] rids = helper.GetString("rids").Split(',');

            for (int i = 0; i < rids.Length; i++)
            {
                if (rids[i] == string.Empty)
                    continue;

                int rid = int.Parse(rids[i]);

                objBLL.UpdateSequence(rid, i * 10);
            }
            response.Write("1");
        }
        catch
        {
            response.Write("0");
        }
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

}